//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Security
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;

    internal class TokenFormatter
    {
        public virtual string Format(Dictionary<string, string> values)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            if (values.Count == 0)
            {
                throw new ArgumentException(@"Values dictionary is empty", "values");
            }

            var builder = new StringBuilder();
            foreach (var key in values.Keys)
            {
                builder.AppendFormat("{0}={1}&", key, HttpUtility.UrlEncode(values[key]));
            }
            
            builder.Remove(builder.Length - 1, 1);
            return builder.ToString();
        }

        public virtual Dictionary<string, string> Parse(string formattedToken)
        {
            if (formattedToken == null)
            {
                throw new ArgumentNullException("formattedToken");
            }

            if (string.IsNullOrWhiteSpace(formattedToken) || formattedToken.EndsWith("&"))
            {
                throw new ArgumentException(@"Invalid formatted token. Must not be empty or end in ampersand", "formattedToken");
            }

            var valuePairs = formattedToken.Split('&');
            var values = new Dictionary<string, string>();
            foreach (var pair in valuePairs)
            {
                var parts = pair.Split('=');
                if (parts.Length != 2)
                {
                    throw new TokenParsingException(pair + " is not a valid expression");
                }

                var key = parts[0];
                var value = HttpUtility.UrlDecode(parts[1]);
                values[key] = value;
            }

            return values;
        }
    }
}
